package com.eqdd.yiqidian.ui.function.learn

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.LearnDemandBean
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.http.JsonConverter
import com.eqdd.yiqidian.ui.gongneng.renshi.train.TrainRequestDetailActivity
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.FlowableBody
import com.shizhefei.mvc.MVCCoolHelper
import io.reactivex.Flowable
import kotlinx.android.synthetic.main.fragment_space.view.*
import org.jetbrains.anko.support.v4.startActivity

/**
 * Created by jhj on 18-9-25.
 */
class LearnDemandFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_space, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val status = arguments?.getInt(Config.STATUS)

        val url = if (status == 0) {
            HttpConfig.BASE_URL + HttpConfig.LEARN_DEMAND_MATCH
        } else {
            HttpConfig.BASE_URL + HttpConfig.LEARN_DEMAND
        }
        var pageNum = 0
        val mvcHelper = MVCCoolHelper<List<LearnDemandBean>>(view.coolRefreshView)
        val list = arrayListOf<LearnDemandBean>()
        view.recyclerView.addItemDecoration(DividerLineItemDecoration(activity))
        view.recyclerView.layoutManager = LinearLayoutManager(activity)
        val slimAdapterEx = SlimAdapterEx.create()
                .register<LearnDemandBean>(R.layout.list_item_193, SlimInjector<LearnDemandBean> { data, injector ->
                    data?.let {
                        injector?.text(R.id.tv_content, data.thetheme)
                                ?.text(R.id.tv_time, "预计培训时间：${data.thedateStart}～${data.thedateEnd}")
                    }
                })
                .attachTo(view.recyclerView)
                .updateData(list as List<*>?) as SlimAdapterEx<LearnDemandBean>?

        ItemClickSupport.addTo(view.recyclerView)
                .setOnItemClickListener { recyclerView, position, v ->
                    val bean = slimAdapterEx?.getDataItem(position)
                    startActivity<TrainRequestDetailActivity>(Config.ID to (bean?.id ?: 0))
                }


        val dataSource = ModelRx2DataSource<LearnDemandBean>(ModelRx2DataSource.OnLoadSource<LearnDemandBean> { page, register ->
            if (page == 1) {
                pageNum = 0
            }
            OkGo.post<HttpResult<List<LearnDemandBean>>>(url)
                    .params("page", pageNum)
                    .params("lectureGuid", (activity as LearnDemandActivity).user.guid)
                    .converter(object : JsonConverter<HttpResult<List<LearnDemandBean>>>() {
                        override fun test() {
                            super.test()
                        }
                    })
                    .adapt(FlowableBody<HttpResult<List<LearnDemandBean>>>())
                    .flatMap({ result ->
                        pageNum = result.page
                        Flowable.just(result.items ?: arrayListOf())
                    })
        })

        mvcHelper.setDataSource(dataSource)
        mvcHelper.adapter = slimAdapterEx
        mvcHelper.refresh()
    }


}